const AdminBiz = require('../../../../../../comm/biz/admin_biz.js');
const pageHelper = require('../../../../../../helper/page_helper.js');
const RequestBiz = require('../../../../../../comm/biz/request_biz.js');
const cacheHelper = require('../../../../../../helper/cache_helper');
const constants = require('../../../../../../comm/constants.js');

Page({

	/**
	 * 页面的初始数据
	 */
	data: {
		isLoad: false,
	},

	/**
	 * 生命周期函数--监听页面加载
	 */
	async onLoad(options) {
		if (!AdminBiz.isAdmin(this)) return;
		if (!pageHelper.getOptions(this, options)) return;
		let rankIdOptions = await this.loadRank();
		this.setData({
			rankIdOptions: rankIdOptions
		});
		this._loadDetail()
	},

	/**
	 * 生命周期函数--监听页面初次渲染完成
	 */
	onReady() {

	},

	/**
	 * 生命周期函数--监听页面显示
	 */
	onShow() {

	},

	/**
	 * 生命周期函数--监听页面隐藏
	 */
	onHide() {

	},

	/**
	 * 生命周期函数--监听页面卸载
	 */
	onUnload() {

	},

	/**
	 * 页面相关事件处理函数--监听用户下拉动作
	 */
	async onPullDownRefresh() {
		await this._loadDetail();
		wx.stopPullDownRefresh();
	},

	/**
	 * 页面上拉触底事件的处理函数
	 */
	onReachBottom() {

	},

	/**
	 * 用户点击右上角分享
	 */
	onShareAppMessage() {

	},

	_loadDetail: async function () {
		if (!AdminBiz.isAdmin(this)) return;

		let id = this.data.id;
		if (!id) return;

		let params = {
			id
		}
		let reqData = await RequestBiz.request('/api/ysjl/user/userInfo', params);
		let user = reqData.data.data;
		if (!user) {
			this.setData({
				isLoad: null,
			})
			return;
		};

		this.setData({
			isLoad: true,
			user
		})
	},

	url: function (e) {
		pageHelper.url(e, this);
	},

	bindSelectCmpt: function (e) {
		let user = this.data.user;
		user.rank = e.detail;
		this.setData({
			user: user
		});
	},

	sliderChanging: function (e) {
		let idx = pageHelper.dataset(e, 'idx');
		let user = this.data.user;
		user.userAbilityList[idx].score = e.detail.value;
		this.setData({
			user
		});
	},

	bindFormSubmit: function (e) {
		let req = RequestBiz.request('/api/ysjl/user/submit',	this.data.user);
		req.then(res => {
			let retData = res.data;
			if (retData.ret == 0) {
				let callback = async function () {
					wx.navigateBack();
				}
				pageHelper.showSuccToast('修改成功', 2000, callback);
			}
		});
	},

	loadRank: async function () {
		let rankList = cacheHelper.get(constants.CACHE_RANK);
		if (rankList) {
			return rankList;
		}
		let resData = await RequestBiz.request("/api/ysjl/user/getDict", {code: 'userRank'});
		rankList = resData.data.data;

		let arr = [];
		for (let k = 0; k < rankList.length; k++) {
			arr.push({
				label: rankList[k].title,
				type: 'rankId',
				val: rankList[k].id,
				value: rankList[k].id,
			})
		}
		cacheHelper.set(constants.CACHE_RANK, arr, constants.CACHE_RANK_EXPIRE);
	},

})